Operaciones con Imagenes Deber 2 Tamia Muñoz
Acceso y Modificacion de Pixeles
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread("KC.jpg")
plt.imshow(img)
<matplotlib.image.AxesImage at 0x1a037302a20>
# Accedemos al valor del pixel en sus 3 canales
px = img[100,100]
px
array([148, 164, 200], dtype=uint8)
# Accediendo al valor de un pixel en un solo canal canal B
px = img[100,100,0]
px
148
# Accediendo al valor de un pixel en un solo canal canal G
px = img[100,100,1]
px
164
# Accediendo al valor de un pixel en un solo canal canal R
px = img[100,100,2]
px
200
Modificacion
# MOdificando valor de pixeles
img[100,100]=[255,255,255]
img[100,100]
array([255, 255, 255], dtype=uint8)
img2 = img[:100, 150:500]
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x1a037410eb8>
# Accediendo
img.item(50,50,2)
68
# Modificando
img.itemset((50,50,2),100)
img.item(50,50,2)
100
Acediendo a las Propiedades de la Imagen Metodo shape
img.shape
(329, 445, 3)
Metodo dtype
img.dtype
dtype('uint8')
type(img)
numpy.ndarray
img.size
439215
ROI de una imagen
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread("KC.jpg")
plt.imshow(img)
<matplotlib.image.AxesImage at 0x1a037479b38>
# extraer la region de interes
mcf = img[210:300,280:360]
plt.imshow(mcf)
<matplotlib.image.AxesImage at 0x1a0377b5748>
img[210:300,200:280]=mcf
plt.imshow(img)
<matplotlib.image.AxesImage at 0x1a037821588>
Dision y Fusion de Canales split()
# split() nos permite dividir los canales de una imagen
b,g,r = cv.split(img)
plt.imshow(b)
plt.title("Blue Channel")
Text(0.5, 1.0, 'Blue Channel')
plt.imshow(g)
plt.title("Green Channel")
Text(0.5, 1.0, 'Green Channel')
plt.imshow(r)
plt.title("Red Channel")
Text(0.5, 1.0, 'Red Channel')
merge()
# merge() permite unir los canales de una imagen
img_unida = cv.merge((b,g,r))
plt.imshow(img_unida)
<matplotlib.image.AxesImage at 0x1a03892bd30>
channel_blue = img[:,:,0]
plt.imshow(channel_blue)
<matplotlib.image.AxesImage at 0x1a038995ef0>
img[:,:,0]=0
plt.imshow(img)
<matplotlib.image.AxesImage at 0x1a038a10128>
Operaciones Aritmeticas con Imagenes Mezcla de Imagenes
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img1 = cv.imread("bn.jpg")
img2 = cv.imread("gt.jpg")
# cambio de espacio de color
img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
img2 = cv.cvtColor(img2, cv.COLOR_BGR2RGB)
# igualar el tamano
fil,cols,chan = img1.shape
img2 = cv.resize(img2,(cols,fil))
#Mostramos las imagenes
plt.figure(1)
plt.imshow(img1)
plt.title("bn")
plt.figure(2)
plt.imshow(img2)
plt.title("gt")
Text(0.5, 1.0, 'gt')
#combinar imagenes
img_out = cv.addWeighted(img1,0.3,img2,0.7,0)
plt.imshow(img_out)
<matplotlib.image.AxesImage at 0x1a038a361d0>
Operaciones bit a bit
#immportamos las librerias
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# cargar nuestras imagenes
img1 = cv.imread('nt.jpg')
img2 = cv.imread('hj.jpg')
# cambiando espacio de color
kurt = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
im2 = cv.cvtColor(img2, cv.COLOR_BGR2RGB)
# mostrado las imagenes leidas
plt.figure(1)
plt.imshow(kurt)
plt.figure(2)
plt.imshow(im2)
<matplotlib.image.AxesImage at 0x1a03a47b940>
# cambiamos el tamano de imagen
fil,col,_ = kurt.shape
fil2,col2,_ = im2.shape
im2 = cv.resize(im2,(col2//2,fil))
plt.imshow(im2)
<matplotlib.image.AxesImage at 0x1a03a3c8860>
fil, col,_ =im2.shape
roi = img1[0:fil,0:col]
plt.imshow(roi)
<matplotlib.image.AxesImage at 0x1a03a501e10>
# creamos una mascara del logotipo
im2_gray = cv.cvtColor(im2, cv.COLOR_RGB2GRAY)
ret,mask = cv.threshold(im2_gray, 150,255, cv.THRESH_BINARY)
plt.imshow(mask, cmap="gray")
<matplotlib.image.AxesImage at 0x1a03a56a470>
# Creamos una mascara invertida
mask_inv = cv.bitwise_not(mask)
plt.imshow(mask_inv, cmap="gray")
<matplotlib.image.AxesImage at 0x1a03a5c1c50>
# tomamos el roi menos la mascara
img1_bg = cv.bitwise_and(roi,roi,mask = mask)
plt.imshow(img1_bg)
<matplotlib.image.AxesImage at 0x1a03b5f5780>
#tomamos region de interes del logotipo im2
img2_bg = cv.bitwise_and(im2,im2,mask=mask_inv)
plt.imshow(img2_bg)
<matplotlib.image.AxesImage at 0x1a03b65c3c8>
img = img1_bg + img2_bg
plt.imshow(img)
<matplotlib.image.AxesImage at 0x1a03a0d6828>
kurt[0:fil,0:col]=img
plt.imshow(kurt)
<matplotlib.image.AxesImage at 0x1a03a41dcf8>
Procesamiento de Imagenes Cambio de Espacio de Color de las Imagenes
# importamos librerias
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread("cs.jpg")
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.imshow(img),plt.title("Imagen Original")
(<matplotlib.image.AxesImage at 0x1a039e89f98>, Text(0.5, 1.0, 'Imagen Original'))
plt.imshow(img[:,:,0]),plt.title("R")
(<matplotlib.image.AxesImage at 0x1a039ef9320>, Text(0.5, 1.0, 'R'))
plt.imshow(img[:,:,1]),plt.title("G")
(<matplotlib.image.AxesImage at 0x1a039f5c940>, Text(0.5, 1.0, 'G'))
plt.imshow(img[:,:,2]),plt.title("B")
(<matplotlib.image.AxesImage at 0x1a039fca080>, Text(0.5, 1.0, 'B'))
YUV:
# importamos librerias
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread("nt.jpg")
img = cv.cvtColor(img, cv.COLOR_BGR2YUV)
plt.imshow(img),plt.title("Imagen Original")
(<matplotlib.image.AxesImage at 0x1a03a02a9b0>, Text(0.5, 1.0, 'Imagen Original'))
plt.imshow(img[:,:,0]),plt.title("Luminosidad")
(<matplotlib.image.AxesImage at 0x1a03a08fda0>, Text(0.5, 1.0, 'Luminosidad'))
plt.imshow(img[:,:,1]),plt.title("U")
(<matplotlib.image.AxesImage at 0x1a03b87dc88>, Text(0.5, 1.0, 'U'))
plt.imshow(img[:,:,2]),plt.title("V")
(<matplotlib.image.AxesImage at 0x1a03a1b30b8>, Text(0.5, 1.0, 'V'))
HSV:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread("op.jpg")
img = cv.cvtColor(img, cv.COLOR_BGR2HSV)
plt.imshow(img),plt.title("Imagen Original")
(<matplotlib.image.AxesImage at 0x1a03a210a20>, Text(0.5, 1.0, 'Imagen Original'))
plt.imshow(img[:,:,0]),plt.title("Canal H")
(<matplotlib.image.AxesImage at 0x1a03a27cf60>, Text(0.5, 1.0, 'Canal H'))
plt.imshow(img[:,:,1]),plt.title("Canal S")
(<matplotlib.image.AxesImage at 0x1a03a2f2748>, Text(0.5, 1.0, 'Canal S'))
plt.imshow(img[:,:,2]),plt.title("Canal V")
(<matplotlib.image.AxesImage at 0x1a03a367048>, Text(0.5, 1.0, 'Canal V'))